package org.liang.email4j.submail;

import org.liang.common.property.PropertiesParser;
import org.liang.email4j.Email4jConfig;

/**
 * Submail 控制类.
 * 
 * <pre>
 * 读取submail 控制文件信息
 * </pre>
 * 
 * @author L.Yang
 * @version v1.0 2015年12月31日 下午2:07:15
 */
public class SubmailConfig extends Email4jConfig {

    /** 默认配置文件名称. */
    private static final String VAL_DEF_PROP_FILE = "email4j-submail.properties";

    /** 默认配置文件包路径. */
    private static final String VAL_DEF_PROP_PACKAGE = "com/tsingsoft/plugin/email4j/submail";

    private static PropertiesParser prop = new PropertiesParser(
                    VAL_DEF_PROP_FILE, VAL_DEF_PROP_PACKAGE);

    /**
     * Three kind of value of {@link #signType}.
     */
    static final String TYPE_NORMAL = "normal";

    static final String TYPE_MD5 = "md5";

    static final String TYPE_SHA1 = "sha1";

    static final String getAppId() {
        return prop.getString("email4j.submail.appid");
    }

    static final String getAppKey() {
        return prop.getString("email4j.submail.appkey");
    }

    /**
     * The method to get the {@link #signType}.It the value from the properties file is not
     * valid,return normal.Otherwise,return the current value.
     * 
     * @return signType that is valid.
     */
    static final String getSignType() {
        String signType = prop.getString("email4j.submail.signtype");
        if (checkType(signType)) {
            return signType;
        }
        return TYPE_NORMAL;
    }

    /**
     * Judge the value of {@link #signType} is valid or not.
     * 
     * @param signType
     *            签名类型
     * @return If the value is inside of {@value #TYPE_MD5},{@value #TYPE_SHA1} and
     *         {@value #TYPE_NORMAL}, return true,otherwise return false;
     */
    private static boolean checkType(String signType) {
        return TYPE_NORMAL.equals(signType) || TYPE_MD5.equals(signType)
               || TYPE_SHA1.equals(signType);
    }
}
